package org.example.Study.Mq.amqp.listener.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.Study.Mq.amqp.Constant.RabbitConstant;
import org.example.Study.Mq.amqp.listener.MessageHander;
import org.example.Study.Mq.amqp.listener.TableCacheListener;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author : zhangbo
 * @data : 2024/6/13
 * @description :
 */
@Component
@Slf4j
public class TableCacheListenerImpl implements TableCacheListener {
    private String ACCEPT;//处理成功
    private String RETRY;//可重试的错误
    private String REJECT;//无需重试的错误
    public static final String QUEUE_SEND_TO_SUBSIDE="queue.send.to.subside";

    //缓存
private ConcurrentHashMap <String, MessageHander>handlers=new ConcurrentHashMap<>();
    @Override
    public void registerMessageHandler(String msgType, MessageHander messageHander) {
    handlers.put(msgType,messageHander);
    }

    @Override
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(durable = "true",autoDelete = "true"),
            exchange = @Exchange(value = RabbitConstant.Queue.QUEUE_SEND_TO_SUBSIDE,type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true",durable = "true"),
            key = RabbitConstant.RoutingKey.ROUTE
    ))
    public void refreshCache(Message message) {
        byte[] body = message.getBody();
        log.info(body.toString());
        MessageHander messageHander = handlers.get("key");
        messageHander.handle(message,null);
    }
}
